{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "617f6a82",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a694eb7",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy as np # shift + Enter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "97ec46dc",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建可以将python中list列表转换成NumPy数组\n",
    "l = [1,2,3,4,5]\n",
    "# NumPy数组\n",
    "nd1 = np.array(l)\n",
    "print(nd1)\n",
    "display(nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5a1f63ea",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape = (3,4),dtype = np.int16)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "93415e4b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),\n",
       " array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),\n",
       " array([[2.718, 2.718, 2.718],\n",
       "        [2.718, 2.718, 2.718]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.ones(10)\n",
    "arr2 = np.zeros(10)\n",
    "arr3 = np.full(shape =[2,3],fill_value=2.718)\n",
    "arr1,arr2,arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cec3b1ee",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.1415 3.1415]]\n"
     ]
    }
   ],
   "source": [
    "arr4 = np.full(shape=[1,2],fill_value=3.1415)\n",
    "print(arr4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "13274b45",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1]], dtype=int16)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape = (3,5),dtype =np.)\n",
    "nd3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6099e88b",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([75, 95, 50,  2, 50, 78, 64, 71,  3, 23, 67, 29, 39, 99, 48, 17, 97,\n",
       "       35, 65, 55])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0,100,size = 20)\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f23ff297",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([90, 20, 88, 33, 80, 83, 46, 24, 32, 73, 24, 58, 81, 83, 81, 27, 63,\n",
       "       13, 13, 60])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0,100,size=20)\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "85d852ef",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7.70465717e-01, 9.95526754e-01, 8.91790722e-02, 9.02812395e-01,\n",
       "        9.76883898e-01],\n",
       "       [6.00353236e-01, 2.87682843e-02, 8.06963156e-04, 3.78728757e-01,\n",
       "        6.53165663e-02],\n",
       "       [6.37916140e-01, 1.01712548e-01, 9.50008597e-03, 8.15567084e-01,\n",
       "        8.50642847e-01]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 =np.random.rand(3,5)\n",
    "nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "723e0678",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.93492019,  0.5241902 ,  0.27045464,  0.04552736, -0.14309836],\n",
       "       [-0.42491752, -0.24724547,  0.21929001,  0.57832827,  0.0057025 ],\n",
       "       [-0.3714304 ,  0.09537795, -1.14650652, -1.67956826,  0.54459506]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd7 = np.random.randn(3,5)\n",
    "nd7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fb93f1f2",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "170.2491302695825\n"
     ]
    }
   ],
   "source": [
    "nd8 = np.random.normal(loc = 175,scale=10)\n",
    "print(nd8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de79f7d8",
   "metadata": {},
   "source": [
    "# 测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ef2ca6",
   "metadata": {},
   "source": [
    "## 测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54ff986f",
   "metadata": {},
   "source": [
    "## "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4480730",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a876f837",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33d1061e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e678edb6",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# numpy的形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05a7ad42",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 数组变形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1eeccac3",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 8, 3, 6],\n",
       "       [6, 1, 4, 9, 5],\n",
       "       [3, 6, 6, 4, 8]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randint(0,10,size = (3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "57b0c3da",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 8],\n",
       "       [3, 6, 6],\n",
       "       [1, 4, 9],\n",
       "       [5, 3, 6],\n",
       "       [6, 4, 8]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "85964c7d",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [8],\n",
       "       [3],\n",
       "       [6],\n",
       "       [6],\n",
       "       [1],\n",
       "       [4],\n",
       "       [9],\n",
       "       [5],\n",
       "       [3],\n",
       "       [6],\n",
       "       [6],\n",
       "       [4],\n",
       "       [8]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9ade2cb4",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[3]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[1]],\n",
       "\n",
       "       [[4]],\n",
       "\n",
       "       [[9]],\n",
       "\n",
       "       [[5]],\n",
       "\n",
       "       [[3]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[4]],\n",
       "\n",
       "       [[8]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15,1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f166cc98",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 8],\n",
       "       [3, 6, 6],\n",
       "       [1, 4, 9],\n",
       "       [5, 3, 6],\n",
       "       [6, 4, 8]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54fe7b95",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d0ab08c8",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 6, 3],\n",
       "       [2, 1, 6],\n",
       "       [8, 4, 6],\n",
       "       [3, 9, 4],\n",
       "       [6, 5, 8]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T #矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "02a5f7ec",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 6, 3],\n",
       "       [2, 1, 6],\n",
       "       [8, 4, 6],\n",
       "       [3, 9, 4],\n",
       "       [6, 5, 8]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a,(1,0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "70c8b708",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[2, 4, 2, 5, 8, 5, 1],\n",
       "        [3, 5, 6, 9, 7, 8, 9],\n",
       "        [0, 3, 9, 8, 9, 7, 2],\n",
       "        [2, 8, 9, 8, 8, 5, 8],\n",
       "        [4, 1, 2, 0, 7, 5, 6]],\n",
       "\n",
       "       [[4, 2, 3, 0, 2, 9, 4],\n",
       "        [2, 4, 9, 3, 0, 2, 6],\n",
       "        [0, 7, 1, 4, 7, 8, 9],\n",
       "        [3, 3, 9, 2, 7, 4, 8],\n",
       "        [6, 5, 3, 3, 2, 7, 1]],\n",
       "\n",
       "       [[0, 3, 7, 1, 1, 9, 0],\n",
       "        [7, 5, 4, 0, 6, 7, 8],\n",
       "        [3, 5, 3, 3, 5, 5, 7],\n",
       "        [8, 5, 6, 2, 4, 8, 5],\n",
       "        [0, 8, 0, 7, 4, 7, 6]]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "b = np.random.randint(0,10,size=(3,5,7))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51cb9b93",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, 5, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b,(2,1,0))\n",
    "c.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f97908e5",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "## 数据堆叠合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "37bd45e2",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 3, 5, 9],\n",
       "       [7, 5, 3, 0, 6],\n",
       "       [4, 8, 9, 1, 9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 6, 2, 0],\n",
       "       [7, 3, 7, 4, 3],\n",
       "       [5, 4, 6, 0, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,10,size=(3,5))\n",
    "nd2 = np.random.randint(0,10,size=(3,5))\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "432903e0",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 3, 5, 9],\n",
       "       [7, 5, 3, 0, 6],\n",
       "       [4, 8, 9, 1, 9],\n",
       "       [8, 8, 6, 2, 0],\n",
       "       [7, 3, 7, 4, 3],\n",
       "       [5, 4, 6, 0, 4]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1,nd2])#默认合并行增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3cd48d21",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 3, 5, 9, 8, 8, 6, 2, 0],\n",
       "       [7, 5, 3, 0, 6, 7, 3, 7, 4, 3],\n",
       "       [4, 8, 9, 1, 9, 5, 4, 6, 0, 4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改axis参数调整数据合并方向\n",
    "np.concatenate([nd1,nd2],axis=1) #axis轴，方向0=行，1=列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "286881b3",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 3, 5, 9, 8, 8, 6, 2, 0],\n",
       "       [7, 5, 3, 0, 6, 7, 3, 7, 4, 3],\n",
       "       [4, 8, 9, 1, 9, 5, 4, 6, 0, 4]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((nd1,nd2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "38e50780",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 3, 5, 9],\n",
       "       [7, 5, 3, 0, 6],\n",
       "       [4, 8, 9, 1, 9],\n",
       "       [8, 8, 6, 2, 0],\n",
       "       [7, 3, 7, 4, 3],\n",
       "       [5, 4, 6, 0, 4],\n",
       "       [8, 8, 6, 2, 0],\n",
       "       [7, 3, 7, 4, 3],\n",
       "       [5, 4, 6, 0, 4],\n",
       "       [8, 8, 6, 2, 0],\n",
       "       [7, 3, 7, 4, 3],\n",
       "       [5, 4, 6, 0, 4],\n",
       "       [4, 2, 3, 5, 9],\n",
       "       [7, 5, 3, 0, 6],\n",
       "       [4, 8, 9, 1, 9]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((nd1,nd2,nd2,nd2,nd1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7b38fb3",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## 数组拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "924587a6",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[48, 35, 79, 54, 82,  7, 27, 61, 64, 78],\n",
       "       [58, 88, 61, 34, 30, 48,  1, 14, 85, 78],\n",
       "       [83, 86, 40, 47, 86, 74, 29, 44,  2, 33],\n",
       "       [46, 75, 40, 70, 93,  7, 38, 40, 44, 29],\n",
       "       [70, 57, 43, 58, 74, 67,  8, 88, 71, 47],\n",
       "       [34,  5, 19, 92, 51, 26, 88, 97, 70,  2],\n",
       "       [87,  2, 84, 95, 54, 65, 54,  1, 81, 96],\n",
       "       [81, 97, 57, 86, 24, 59, 77, 56, 54, 44],\n",
       "       [29, 42, 59, 29, 56, 46, 81, 87, 86, 71],\n",
       "       [52, 10,  5, 62, 60, 86, 57, 70, 19, 33],\n",
       "       [71, 55, 66, 41, 20, 76, 81,  0, 89, 80],\n",
       "       [27, 48, 25, 21, 56, 61, 66, 47,  9, 57],\n",
       "       [30, 11, 83, 82, 64, 89, 75, 31, 21, 41],\n",
       "       [74, 15, 87, 19,  0, 28, 39, 42, 61, 46],\n",
       "       [41, 77, 65, 14, 65, 84, 92, 10, 82, 19]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size=(15,10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e022cec5",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[48, 35, 79, 54, 82,  7, 27, 61, 64, 78],\n",
       "        [58, 88, 61, 34, 30, 48,  1, 14, 85, 78],\n",
       "        [83, 86, 40, 47, 86, 74, 29, 44,  2, 33]]),\n",
       " array([[46, 75, 40, 70, 93,  7, 38, 40, 44, 29],\n",
       "        [70, 57, 43, 58, 74, 67,  8, 88, 71, 47],\n",
       "        [34,  5, 19, 92, 51, 26, 88, 97, 70,  2]]),\n",
       " array([[87,  2, 84, 95, 54, 65, 54,  1, 81, 96],\n",
       "        [81, 97, 57, 86, 24, 59, 77, 56, 54, 44],\n",
       "        [29, 42, 59, 29, 56, 46, 81, 87, 86, 71]]),\n",
       " array([[52, 10,  5, 62, 60, 86, 57, 70, 19, 33],\n",
       "        [71, 55, 66, 41, 20, 76, 81,  0, 89, 80],\n",
       "        [27, 48, 25, 21, 56, 61, 66, 47,  9, 57]]),\n",
       " array([[30, 11, 83, 82, 64, 89, 75, 31, 21, 41],\n",
       "        [74, 15, 87, 19,  0, 28, 39, 42, 61, 46],\n",
       "        [41, 77, 65, 14, 65, 84, 92, 10, 82, 19]])]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=5) #给数字，表示平均分成多少份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5c73e5d7",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[48, 35, 79, 54, 82],\n",
       "        [58, 88, 61, 34, 30],\n",
       "        [83, 86, 40, 47, 86],\n",
       "        [46, 75, 40, 70, 93],\n",
       "        [70, 57, 43, 58, 74],\n",
       "        [34,  5, 19, 92, 51],\n",
       "        [87,  2, 84, 95, 54],\n",
       "        [81, 97, 57, 86, 24],\n",
       "        [29, 42, 59, 29, 56],\n",
       "        [52, 10,  5, 62, 60],\n",
       "        [71, 55, 66, 41, 20],\n",
       "        [27, 48, 25, 21, 56],\n",
       "        [30, 11, 83, 82, 64],\n",
       "        [74, 15, 87, 19,  0],\n",
       "        [41, 77, 65, 14, 65]]),\n",
       " array([[ 7, 27, 61, 64, 78],\n",
       "        [48,  1, 14, 85, 78],\n",
       "        [74, 29, 44,  2, 33],\n",
       "        [ 7, 38, 40, 44, 29],\n",
       "        [67,  8, 88, 71, 47],\n",
       "        [26, 88, 97, 70,  2],\n",
       "        [65, 54,  1, 81, 96],\n",
       "        [59, 77, 56, 54, 44],\n",
       "        [46, 81, 87, 86, 71],\n",
       "        [86, 57, 70, 19, 33],\n",
       "        [76, 81,  0, 89, 80],\n",
       "        [61, 66, 47,  9, 57],\n",
       "        [89, 75, 31, 21, 41],\n",
       "        [28, 39, 42, 61, 46],\n",
       "        [84, 92, 10, 82, 19]])]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=2,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a0c428bf",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[48, 35, 79, 54, 82,  7, 27, 61, 64, 78]]),\n",
       " array([[58, 88, 61, 34, 30, 48,  1, 14, 85, 78],\n",
       "        [83, 86, 40, 47, 86, 74, 29, 44,  2, 33],\n",
       "        [46, 75, 40, 70, 93,  7, 38, 40, 44, 29],\n",
       "        [70, 57, 43, 58, 74, 67,  8, 88, 71, 47]]),\n",
       " array([[34,  5, 19, 92, 51, 26, 88, 97, 70,  2],\n",
       "        [87,  2, 84, 95, 54, 65, 54,  1, 81, 96],\n",
       "        [81, 97, 57, 86, 24, 59, 77, 56, 54, 44],\n",
       "        [29, 42, 59, 29, 56, 46, 81, 87, 86, 71]]),\n",
       " array([[52, 10,  5, 62, 60, 86, 57, 70, 19, 33],\n",
       "        [71, 55, 66, 41, 20, 76, 81,  0, 89, 80],\n",
       "        [27, 48, 25, 21, 56, 61, 66, 47,  9, 57],\n",
       "        [30, 11, 83, 82, 64, 89, 75, 31, 21, 41],\n",
       "        [74, 15, 87, 19,  0, 28, 39, 42, 61, 46],\n",
       "        [41, 77, 65, 14, 65, 84, 92, 10, 82, 19]])]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#参数给列表，根据列表中的索引，进行切片\n",
    "np.split(a,indices_or_sections=[1,5,9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "50d4c4c6",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[48, 35, 79, 54, 82],\n",
       "        [58, 88, 61, 34, 30],\n",
       "        [83, 86, 40, 47, 86],\n",
       "        [46, 75, 40, 70, 93],\n",
       "        [70, 57, 43, 58, 74],\n",
       "        [34,  5, 19, 92, 51],\n",
       "        [87,  2, 84, 95, 54],\n",
       "        [81, 97, 57, 86, 24],\n",
       "        [29, 42, 59, 29, 56],\n",
       "        [52, 10,  5, 62, 60],\n",
       "        [71, 55, 66, 41, 20],\n",
       "        [27, 48, 25, 21, 56],\n",
       "        [30, 11, 83, 82, 64],\n",
       "        [74, 15, 87, 19,  0],\n",
       "        [41, 77, 65, 14, 65]]),\n",
       " array([[ 7, 27, 61, 64, 78],\n",
       "        [48,  1, 14, 85, 78],\n",
       "        [74, 29, 44,  2, 33],\n",
       "        [ 7, 38, 40, 44, 29],\n",
       "        [67,  8, 88, 71, 47],\n",
       "        [26, 88, 97, 70,  2],\n",
       "        [65, 54,  1, 81, 96],\n",
       "        [59, 77, 56, 54, 44],\n",
       "        [46, 81, 87, 86, 71],\n",
       "        [86, 57, 70, 19, 33],\n",
       "        [76, 81,  0, 89, 80],\n",
       "        [61, 66, 47,  9, 57],\n",
       "        [89, 75, 31, 21, 41],\n",
       "        [28, 39, 42, 61, 46],\n",
       "        [84, 92, 10, 82, 19]])]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,indices_or_sections=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fa85f18b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[48, 35, 79, 54, 82,  7, 27, 61, 64, 78],\n",
       "        [58, 88, 61, 34, 30, 48,  1, 14, 85, 78],\n",
       "        [83, 86, 40, 47, 86, 74, 29, 44,  2, 33]]),\n",
       " array([[46, 75, 40, 70, 93,  7, 38, 40, 44, 29],\n",
       "        [70, 57, 43, 58, 74, 67,  8, 88, 71, 47],\n",
       "        [34,  5, 19, 92, 51, 26, 88, 97, 70,  2],\n",
       "        [87,  2, 84, 95, 54, 65, 54,  1, 81, 96]]),\n",
       " array([[81, 97, 57, 86, 24, 59, 77, 56, 54, 44],\n",
       "        [29, 42, 59, 29, 56, 46, 81, 87, 86, 71],\n",
       "        [52, 10,  5, 62, 60, 86, 57, 70, 19, 33],\n",
       "        [71, 55, 66, 41, 20, 76, 81,  0, 89, 80]]),\n",
       " array([[27, 48, 25, 21, 56, 61, 66, 47,  9, 57],\n",
       "        [30, 11, 83, 82, 64, 89, 75, 31, 21, 41],\n",
       "        [74, 15, 87, 19,  0, 28, 39, 42, 61, 46],\n",
       "        [41, 77, 65, 14, 65, 84, 92, 10, 82, 19]])]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,indices_or_sections=[3,7,11])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b699b43c",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d0ad0d8b",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0,1,2,3]*3)\n",
    "arr1.sort() # 排序，从小到大\n",
    "arr1 = arr1.reshape(4,3)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "aeb8e207",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.array([1,2,3])\n",
    "display(arr1,arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "483db265",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#行不够，广播行\n",
    "arr1 + arr2 #arr2和arr1中每一行，进行相加：广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9afc1724",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([[1],[2],[3],[4]])\n",
    "display(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "56515fb4",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) # shape(4,3)\n",
    "arr2 = np.array([[1],[2],[3],[4]]) # shape(4,1)\n",
    "arr3 = arr1 + arr2 # arr2 进行广播复制3份 shape(4,3)\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16f17881",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2) #shape(3,4,2)\n",
    "arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2)\n",
    "arr3 = arr1 + arr2 # arr2数组在0维上复制3份 shape(3,4,2)\n",
    "arr3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e569ac66",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cca4f26",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## 元素级数字函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8660f68b",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  4.,  8.,  9., 16., 16.])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([1,4,8,9,16,25])\n",
    "arr1 = np.sqrt(arr1) # 开平方\n",
    "arr1\n",
    "arr1 = np.square(arr1) # 平方\n",
    "arr1\n",
    "arr1 = np.clip(arr1,2,16) # 输出 array([ 2, 4, 8, 9, 16, 16])\n",
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "339f24fc",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## 数学统计和计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "623bdc63",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[87, 50, 59, 96, 75],\n",
       "       [69, 68, 96, 56, 62],\n",
       "       [27, 36,  0,  9, 68]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.randint(0,100,size=(3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5b02315f",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a7e2eda",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([87, 68, 96, 96, 75])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis=0) #axis轴，方向。axis=0行，axis=1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72e7cae9",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([96, 96, 68])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "11d6ac87",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "57.2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean() #平均数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9ed8a1d8",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "62.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(a) #中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c01b706",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "858"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum() #求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5d3ab4eb",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27.83810338367181"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std() #标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cecc9bdd",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "774.9599999999999"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var() #方差，数据内部波动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "27fed200",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 87, 137, 196, 292, 367, 436, 504, 600, 656, 718, 745, 781, 781,\n",
       "       790, 858], dtype=int32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum() #累加和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8f7ba981",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([         87,        4350,      256650,    24638400,  1847880000,\n",
       "       -1345298880, -1286010624,  1097031680,  1304231936,  -741998592,\n",
       "        1440874496,   331874304,           0,           0,           0],\n",
       "      dtype=int32)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumprod() #累乘和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4a872521",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin() #最小值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cec57aa3",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax() #最大值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8cf17779",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 2],\n",
       "       [0, 3],\n",
       "       [0, 4],\n",
       "       [1, 0],\n",
       "       [1, 1],\n",
       "       [1, 2],\n",
       "       [1, 3],\n",
       "       [1, 4],\n",
       "       [2, 4]], dtype=int64)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere(a>50) #返回就是符合条件的索引\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7a6d8648",
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87\n",
      "59\n",
      "96\n",
      "75\n",
      "69\n",
      "68\n",
      "96\n",
      "56\n",
      "62\n",
      "68\n"
     ]
    }
   ],
   "source": [
    "for i,j in index:\n",
    "    print(a[i,j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "501f0ea7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 363.3 , -167.6 ,  -40.75],\n",
       "       [-167.6 ,  235.2 , -199.25],\n",
       "       [ -40.75, -199.25,  702.5 ]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cov协方差（属性之间进行计算），方差概念类似（数据内部，属性内部计算）\n",
    "np.cov(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "18944719",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.57335375, -0.08066251],\n",
       "       [-0.57335375,  1.        , -0.49018104],\n",
       "       [-0.08066251, -0.49018104,  1.        ]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(a)  #相关系数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f56ffe57",
   "metadata": {},
   "source": [
    "# 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "243fad9f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39f44a95",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb8679a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
